home *** CD-ROM | disk | FTP | other *** search
- /*
- * Chapter 4 Sample Application
- *
- */
-
- /************************************
- * Inclusions
- ************************************/
- #ifdef THINK_C
- #include <ColorToolbox.h>
- #include <DialogMgr.h>
- #include <ResourceMgr.h>
- #include <EventMgr.h>
- #include <MenuMgr.h>
- #endif
-
- #ifdef applec
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Menus.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Desk.h>
- #include <Fonts.h>
-
- #define thePort qd.thePort
- #endif
-
- #include "BigEasy2.h"
-
- /************************************
- * Types and globals
- ************************************/
- Rect dOffBounds = {0,0,200,200};
-
- DrawSplug()
- /*
- * Draws the window.
- */
- {
- EraseRect(&gBigRect);
- MoveTo(20,20);
- DrawString("\pUse Memory Menu.");
-
- }
-
- ClickSplug(n,p)
- /*
- * Come here for a click in the window.
- */
- short n; /* window number */
- Point p; /* where clicked */
- {
- #ifdef applec
- #pragma unused (n,p);
- #endif
- }
-
- GoAwaySplug(n)
- /*
- * Close that window...
- */
- short n;
- {
- UninstallWindow(n);
- }
-
- void ActivateSplug(n)
- {
- #ifdef applec
- #pragma unused (n);
- #endif
- SetMenuItem(23,true,0,0,nil); /* enable "Close" menu item */
- SetMenuItem(24,false,0,0,nil); /* disable "Open" menu item */
- KillEds();
- }
-
- void DeactivateSplug(n)
- {
- #ifdef applec
- #pragma unused (n);
- #endif
- SetMenuItem(23,false,0,0,nil); /* disable "Close" menu item */
- SetMenuItem(24,true,0,0,nil); /* enable "Open" menu item */
- }
-
- void pnull(){}
- letsquit()
- {
- gQuitApp++;
- }
-
- KillEds()
- {
- EnDisEdits(0,0,0,0,0);
- }
-
- OpenWindow()
- {
- Rect r;
-
- SetRect(&r,100,100,100+dOffBounds.right,100+dOffBounds.bottom);
-
- InstallWindow(1,"\pWindow",&r,0,1,
- (FP)DrawSplug,(FP)ClickSplug,(FP)pnull,(FP)GoAwaySplug,
- (FP)ActivateSplug,(FP)DeactivateSplug,(FP)pnull, false);
- }
-
- CorruptHeap()
- {
- long size=0x100;
- short iii;
- Handle myHandle;
-
- if( PutUpMessage( 1, "\pThe heap becomes corrupted. Use HD to find corrupted block. Use ES to continue when Mac crashes." ) )
- {
- DebugStr("\pAllocates a handle and fills it with 5 ';hc;g" );
- myHandle = NewHandle( size );
- for( iii = 0; iii <=size; iii++ )
- ((short *)*myHandle)[iii] = (short) 5;
-
- DebugStr("\pHeap is corrupted! Use HD to find corrupted block, ES to continue ';hc;g" );
- DisposHandle( myHandle );
- }
- }
-
- FragmentHeap()
- {
- long size;
- long memAvail = MaxMem( &size );
- Ptr myPtr1;
- Ptr myPtr2;
- Handle myHandle;
-
- if( PutUpMessage( 1, "\pNewHandle will fail due to a fragmented heap. " ) )
- {
- if( memAvail + size > 8000 )
- {
- myPtr1 = NewPtr( (memAvail+size)/2 );
- myPtr2 = NewPtr( 4000 ); /* ptr2 is now in the middle of the heap */
- DisposPtr( myPtr1 );
-
- DebugStr("\pNext NewHandle fails even though there is enough memory in heap" );
- if( !(myHandle = NewHandle( (memAvail+size + 500)/2 ) ) )
- DebugStr("\pMemory allocation failed due to fragmented heap. Use HD to see heap." );
- else
- DisposHandle( myHandle );
-
- DisposPtr( myPtr2 );
- }
- }
- }
-
- MemoryLeak()
- {
- Handle myHandle;
-
- if( PutUpMessage( 1, "\pThis causes some memory to be leaked, eventually filling memory with allocated but unused blocks." ) )
- {
- myHandle = NewHandle( 4000 );
- }
- }
-
- InitVars()
- /*
- * Called once at startup: yes, it
- * inits the vars.
- */
- {
- }
-
- void Bootstrap()
- {
- InitVars();
-
- /*** Menu 2 ***/
- InstallMenu("\pFile",(FP)pnull,0);
- InstallItem("\pOpen/O",(FP)OpenWindow,23);
- InstallItem("\pClose/W",(FP)GoAwaySplug,-24);
- InstallItem("\p(-",(FP)pnull,0);
- InstallItem("\pQuit/Q",(FP)letsquit,0);
-
- /*** Menu 3 ***/
- InstallEditMenu((FP)pnull,(FP)pnull,(FP)pnull,(FP)pnull,(FP)pnull);
-
- /*** Menu 4 ***/
- InstallMenu("\pMemory",(FP)pnull,0);
- InstallItem("\pCorrupt Heap",(FP)CorruptHeap,25);
- InstallItem("\pFragment Heap",(FP)FragmentHeap,29);
- InstallItem("\pLeak Some Memory",(FP)MemoryLeak,26);
-
- KillEds();
- }
-
-
-
-